package com.turing.manage.login;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * @desc   登录模块的C层
 * @author fx
 * @time   2021-12-11
 */
@SuppressWarnings("serial")
public class LoginServlet extends HttpServlet{
	
	LoginService loginService = new LoginService();
	HttpSession session;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
				//0.基础设置和接值
				request.setCharacterEncoding("utf-8");
				response.setCharacterEncoding("utf-8");
				String method = request.getParameter("method");
		
				if ("login".equals(method))
				{
					this.login(request,response);
				}
				else if ("logout".equals(method))
				{
					this.logout(request,response);
				}
			} 
		catch (ClassNotFoundException | SQLException e)
		{
			System.out.println("1.首先打印出异常信息:"+e.getMessage());
			System.out.println("2.打印出异常发生的内存地址:"+e.getStackTrace());
			//3.转向错误处理页面，并同时把异常信息传递过去
			request.setAttribute("errorMsg",e.getMessage());
			request.getRequestDispatcher("/manage/error.jsp").forward(request, response);
		}
	}
	
	/**
	 * @desc  2.退出
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		session.invalidate();
		System.out.println("当前用户的session已经销毁");
		request.getRequestDispatcher("/manage/login.jsp").forward(request, response);
	}

	/**
	 * @desc  1.登录验证
	 * @param request
	 * @param response
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void login(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, SQLException, ServletException, IOException {
		
		//1.接值
		String name = request.getParameter("name");
		String pass = request.getParameter("pass");
		
		//2.调用M层中的方法进行登录验证
		Map<String,Object> map = loginService.queryByNameAndPass(name,pass);
		
		//立足request获取session对象
		 session = request.getSession();
		
		if (map!=null) {
			//3.存值
			request.setAttribute("map", map);
			session.setAttribute("user",map);
			String sessionId = session.getId();
			System.out.println("session创建成功,session的id是:"+sessionId);
			//response.getWriter().print("session创建成功,session的id是:"+sessionId);
			
			//4.转向
			//request.getRequestDispatcher("/manage/manager/list.jsp").forward(request, response);
			//重定向
			response.sendRedirect(request.getContextPath()+"/manage/manager.do?method=query");
			//response.sendRedirect(request.getContextPath()+"/manage/classs.do?method=query");
		}
		else{
			request.setAttribute("errorMsg", "您输入的用户名或者密码不正确，请重新输入");//错误信息存入到request作用域中，目的是更加清晰的提示用户进行操作
			//4.转向
			request.getRequestDispatcher("/manage/error.jsp").forward(request, response);
		}
	}
	
	
}
